 <?php
/**
 * @description : choose a payment gateway for user.
 * @author      : stephen.mo
 * @date        : 2010/09/02
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set('display_errors','on');

$current_page     =  OAuthConfig::PAGE_PLAN_CMODEL;
$login_id         = (isset($_REQUEST['login_id'])) ? $_REQUEST['login_id'] : NULL;
$access_token     = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
$call_back        = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : OAuthConfig::OAuthGetRestUrl($_SERVER);
$lang             = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$pukey            = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
$lid              = (isset($_REQUEST['lid'])) ? $_REQUEST['lid'] : NULL;
$login_id         = (empty($login_id) && !empty($lid)) ? OAuthUtils::getDefaultLoginId($lid) : $login_id;

//setting the action url for choose model.
$base_server_path = OAuthConfig::OAuthDomainUrl($_SERVER);
$base_back_url    = OAuthConfig::OAuthGetBackUrl($_SERVER);
$reset_page_url   = OAuthConfig::OAuthGetRestUrl($_SERVER);
$jqtouch_url      = $base_server_path.OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE;
$oauth_token_str  = (!empty($access_token)) ? '&oauth_token='.$access_token : NULL;
$login_id_str     = (!empty($login_id)) ? '&login_id='.$login_id : NULL;
$reback_url       = $base_server_path.OAuthConfig::DEFAULT_CMODEL_URL.'?call_back='.urlencode($call_back).$oauth_token_str.$login_id_str.'&lang='.$lang;
$action_url       = $base_server_path.OAuthConfig::DEFAULT_CHOOSE_PLAN_URL.'?call_back='.urlencode($call_back).'&reback_url='.urlencode($reback_url);
$back_url_top     = $base_server_path.OAuthConfig::DEFAULT_HEADER_URL.'?oauth_reback='.urlencode($call_back);
$plan_request_url = $base_server_path.OAuthConfig::DEFAULT_REQUEST_PAGE;
$message_url      = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;

//setting vars.
$params = array();
$params['call_back']  = urlencode($call_back);
$params['reback_url'] = urlencode($reback_url);
$params['lang']       = $lang;

//verify the oauth token.
if (OAuthModelConfig::IS_NEED_CHECK_OAUTHTOKEN) {
    $ret = OAuthUtils::verifyAccessToken($access_token);
    if (!$ret || !$ret['flag']) {
        $other_msg = array('call_back'=>(isset($call_back) ? urlencode($call_back) : urlencode($reset_page_url)) );
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1005,1006),$other_msg);
    }
    //try to get login id from oauth token vars.
    if (empty($login_id)) {
        $user = unserialize($ret['data']['data']);
        $login_id = (!empty($user)) ? OAuthUtils::getLoginIdByEmail($user->user) : NULL;
    }
}

//get ptoken & pukey & verify it.
$ptoken = OAuthUtils::getPToken();
if (empty($ptoken) || empty($pukey)) {
    $pukey  = NULL;
    $ptoken = NULL;
    $pukeyInfo = OAuthUtils::genFormPUKey();
    if ($pukeyInfo->retFlag==1) {
        $keyInfo = $pukeyInfo->data;
        $pukey   = $keyInfo->pukey;
        $ptoken  = $keyInfo->ptoken;
        OAuthUtils::setPToken($ptoken);
    }
}
elseif (!empty($ptoken) && !empty($pukey)) {
    $pukey = OAuthUtils::reGenPUKey($pukey,$ptoken);
}
//set data to memcache.
$fdata = array();
$fdata['m_login_id']     = $login_id;
$fdata['current_page']   = $current_page;
$fdata['m_call_back']    = $call_back;
$fdata['m_lang']         = $lang;
$fdata['m_reback_url']   = $reback_url;
$fdata['m_step_back_I']  = $base_back_url;
$ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
if (!$ret) {
    //not success need to retry it.
    $retry_url = OAuthConfig::OAuthGetBuyPlanFirst($params);
    $other_msg = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1000),$other_msg);
    exit;
}

//redirect url setting.
$redirect_url       = $action_url.$oauth_token_str.$login_id_str.'&lang='.$lang.'&pukey='.$pukey;
$thrid_payment_vars = '?call_back='.urlencode($base_back_url).'&lang='.$lang;

//page info vars
$choose_model_page  = OPI::$choose_model_info;
$payment_buttons    = OPI::$payment_button_info;
$select_model_vars  = OAuthUtils::getModelOfPayment();

//setting the form datas into array.
$form = array();
$using_real_env               = OAuthModelConfig::USING_TEMPLATE_ON_REAL_ENV;
$page                         = OAuthModelConfig::M_PAGE_OF_CMODEL;
$form['lang']                 = $lang;
$form['title']                = $choose_model_page[$lang]['page_title'];
$form['cmodel_title']         = $choose_model_page[$lang]['cmodel_title'];
$form['redirect_url']         = $plan_request_url;
$form['lid']                  = (!empty($login_id)) ? base64_encode($login_id) : NULL;
$form['m_call_back_url']      = $reset_page_url;

//tvb.com asia payment.
$form['card_payment']         = $choose_model_page[$lang]['cmodel_cardpayment'];
$form['third_payment']        = $choose_model_page[$lang]['cmodel_thirdpayment'];
$form['payment_gateway_list'] = $select_model_vars; 

//tvb.com 
$form['asia_pay_tvb']         = array('link'=>$redirect_url.'&model=TVB',
                                      'name'=>'tvb.com',
                                      'desc'=>$choose_model_page[$lang]['cmodel_descrption_for_tvbpay']);
//buttons.
$form['button']['cancel']     = array('value'=>$payment_buttons[$lang]['cancel'],
                                      'link'=>$back_url_top);

//include thie template for plan cmodel page.
include "..".OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE.OAuthModelConfig::$mobile_teplate_page[$page];

?>
